﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Common;

/// <summary>
/// Summary description for BasicDaoSql
/// </summary>
public class BasicDaoSql
{
    public static string SCHEMA = "dbo";

    public BasicDaoSql()
    {
        this.ConnectionString = UtilSettings.AuthDatabase.ConnectionString;
        this.ProviderName = UtilSettings.AuthDatabase.ProviderName;
    }

    string providerName = "";
    public string ProviderName
    {
        get
        {
            return providerName;
        }
        set
        {
            providerName = value;
        }
    }

    string connectionString = "";
    public string ConnectionString
    {
        get
        {
            return connectionString;
        }
        set
        {
            connectionString = value;
        }
    }
    protected DbConnection getDatabaseConnection()
    {
        DbProviderFactory dbProviderFactory = DbProviderFactories.GetFactory(providerName);

        DbConnection con = dbProviderFactory.CreateConnection();
        con.ConnectionString = this.ConnectionString;
        con.Open();
        //logger.debug("Ending in BasicDaoOracle.getDatabaseConnection");

        return con;
    }
    protected string getParameterString(string para)
    {
        string result = "";

        switch (this.ProviderName)
        {
            case "System.Data.SqlClient":
                result = "@" + para;
                break;

            case "System.Data.OleDb":
                result = "?";
                break;
            case "System.Data.OracleClient":
                result = "" + para;
                break;
        }
        return result;
    }
    protected DbParameter[] CreateDBParameter(DbCommand cStmt, int paramNo)
    {
        DbParameter[] para = new DbParameter[paramNo];
        for (int j = 0; j < paramNo; j++)
            para[j] = cStmt.CreateParameter();

        return para;
    }
    protected void PopulateDBParameter(DbParameter para, String fieldName, Object value)
    {
        para.ParameterName = getParameterString(fieldName.Trim());

        Object tempValue = value;
        if (value is DateTime)
        {
            DateTime obj = (DateTime)value;
            tempValue = getProperDate(obj);
        }
        else if (value is string && Convert.ToString(value) == "")
        {
            tempValue = " ";
        }
        else if (value is string)
        {
            tempValue = Convert.ToString(value);
        }
        else if (value == null)
        {
            tempValue = System.DBNull.Value;
        }
        para.Value = tempValue;
    }

    protected void PopulateDBParameterNoCase(DbParameter para, String fieldName, Object value)
    {
        para.ParameterName = getParameterString(fieldName.Trim());

        Object tempValue = value;
        if (value is DateTime)
        {
            DateTime obj = (DateTime)value;
            tempValue = getProperDate(obj);
        }
        else if (value is string && Convert.ToString(value) == "")
        {
            tempValue = " ";
        }
        else if (value == null)
        {
            tempValue = System.DBNull.Value;
        }
        para.Value = tempValue;
    }

    protected void PopulateDBParameterToUpper(DbParameter para, String fieldName, string value)
    {
        para.ParameterName = getParameterString(fieldName.Trim());

        object tempValue = value;
        if (value == null)
        {
            tempValue = System.DBNull.Value;
        }
        else if (value == "")
        {
            tempValue = " ";
        }
        else
        {
            tempValue = value.ToUpper();
        }
        para.Value = tempValue;
    }

    protected object getProperDate(DateTime para)
    {
        if (para != DateTime.MinValue)
            return para;
        else
            return System.DBNull.Value;
    }
    protected int getIntValue(DbDataReader rs, String fieldName)
    {
        return (rs[fieldName.Trim()] == System.DBNull.Value ? 0 : Convert.ToInt32(rs[fieldName.Trim()]));
    }
    protected Decimal getDecimalValue(DbDataReader rs, String fieldName)
    {
        return (rs[fieldName.Trim()] == System.DBNull.Value ? 0 : Convert.ToDecimal(rs[fieldName.Trim()]));
    }
    protected short getShortValue(DbDataReader rs, String fieldName)
    {
        return (rs[fieldName.Trim()] == System.DBNull.Value ? (short)0 : Convert.ToInt16(rs[fieldName.Trim()]));
    }
    protected double getDoubleValue(DbDataReader rs, String fieldName)
    {
        return (rs[fieldName.Trim()] == System.DBNull.Value ? 0 : Convert.ToDouble(rs[fieldName.Trim()]));
    }
    protected string getStringValue(DbDataReader rs, String fieldName)
    {
        return (rs[fieldName.Trim()] == System.DBNull.Value ? "" : Convert.ToString(rs[fieldName.Trim()]));
    }
    protected DateTime getDateTimeValue(DbDataReader rs, String fieldName)
    {
        return (rs[fieldName.Trim()] == System.DBNull.Value ? DateTime.MinValue : Convert.ToDateTime(rs[fieldName.Trim()]));
    }
    protected long getLongValue(DbDataReader rs, String fieldName)
    {
        return (rs[fieldName.Trim()] == System.DBNull.Value ? (long)0 : Convert.ToInt64(rs[fieldName.Trim()]));
    }
    protected object getObjectValue(DbDataReader rs, String fieldName)
    {
        return (rs[fieldName.Trim()] == System.DBNull.Value ? null : rs[fieldName.Trim()]);
    }
    protected string addQueryArray(string fieldName, string[] values, bool withIn)
    {
        string newQuery = "";
        if (values != null)
        {
            for (int i = 0; i < values.Length; i++)
            {
                if (i == 0)
                {
                    if (withIn)
                    {
                        newQuery += " " + fieldName + " in (";
                    }
                    else
                    {
                        newQuery += " " + fieldName + " not in (";
                    }
                }
                newQuery += "'" + values[i] + "'";
                if (i == values.Length - 1)
                {
                    newQuery += ")";
                }
                else
                {
                    newQuery += ",";
                }
            }
        }
        return newQuery;
    }
    protected string addQueryArray(string fieldName, int[] values, bool withIn)
    {
        string newQuery = "";
        for (int i = 0; i < values.Length; i++)
        {
            if (i == 0)
            {
                if (withIn)
                {
                    newQuery += " " + fieldName + " in (";
                }
                else
                {
                    newQuery += " " + fieldName + " not in (";
                }
            }
            newQuery += values[i];
            if (i == values.Length - 1)
            {
                newQuery += ")";
            }
            else
            {
                newQuery += ",";
            }
        }
        return newQuery;
    }
    public void AddRange(DbCommand pstmt, DbParameter[] para)
    {
        for (int i = 0; i < para.Length; i++)
        {
            pstmt.Parameters.Add(para[i]);
        }
    }
}
